package com.bizfx.component.controls
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.containers.HDividedBox;
	import mx.containers.dividedBoxClasses.BoxDivider;
	import mx.controls.Image;
	import mx.effects.Move;
	import mx.effects.Resize;
	import mx.events.DividerEvent;
	import mx.events.FlexEvent;
	import mx.managers.CursorManager;
	
	public class AutoHDividedBox extends HDividedBox
	{
		[Embed(source="assets/images/deviderbox/splitter_l.gif")]
		private var _middle_icon_left:Class;
		[Embed(source="assets/images/deviderbox/splitter_r.gif")]
		private var _middle_icon_right:Class;
		
		private var divLocals:Array = new Array();
		
		private var dividerBoxes:Array = new Array();
		
		private var _move:Move;
		
		private var _resize:Resize;
		
		public function AutoHDividedBox()
		{
			super();
			_move = new Move;
			_resize = new Resize;
			liveDragging = true;
			this.addEventListener(DividerEvent.DIVIDER_DRAG,dividerDragHandle);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private var _duration:Number=1000;
		
		/**
		 * 效果时间
		 */
		public function get duration():Number{
			return this._duration;
		}
		
		public function set duration(value:Number):void{
			this._duration =value;
			if(_move)
				_move.duration = value;
			if(_resize)
				_resize.duration = value;
		}
		
		override protected function createChildren():void {
			super.createChildren();
		}
		
		private function init(event:FlexEvent):void{
			if(this.getChildren().length < 2){
				return;
			}
			
			for (var i:int; i < this.getChildren().length - 1; i++) {
				var divider:BoxDivider = this.getDividerAt(i);
				_move.target = divider;
				_resize.target = this.getChildAt(i);
				divLocals[i] = divider.x;
				
				var dividerBox:Image = new Image();
				this.dividerBoxes[i] = dividerBox;
				dividerBox.addEventListener(MouseEvent.MOUSE_DOWN, dividerClickHandler);
				dividerBox.addEventListener(MouseEvent.MOUSE_OVER,
					function(event:MouseEvent):void {
						CursorManager.removeAllCursors();
						event.stopImmediatePropagation();
					});
				dividerBox.source = _middle_icon_left;
				dividerBox.width = 9;
				dividerBox.height = 79;
				dividerBox.x = -3;
				dividerBox.y = (divider.height - dividerBox.height)/2;
				divider.addChild(dividerBox);	
			}
		}
		
		private function dividerDragHandle(event:DividerEvent):void{
//			if(this.getDividerAt(0).x>2){
//				divider.source = _middle_icon_left;
//			}else{
//				divider.source = _middle_icon_right;
//			}
		}
		private function dividerClickHandler(e:MouseEvent):void {
			e.stopImmediatePropagation();
			
			var i:int = 0;
			for (i; i < this.getChildren().length - 1; i++) {
				if (this.getDividerAt(i) == e.target.parent) {
					break;
				}
			}
			var divider:BoxDivider = this.getDividerAt(i);
			_move.target = divider;
			_resize.target = this.getChildAt(i);
			if(this.dividerBoxes[i].source == _middle_icon_left){
				divLocals[i] = divider.x;
				if (i > 0) {
					_move.xTo = divLocals[i - 1];
					_resize.widthTo = _move.xTo;
				} else {
					_move.xTo = 0;
					_resize.widthTo = 0;
				}
				_move.play();
				_resize.play();
				this.dividerBoxes[i].source = _middle_icon_right;
			} else{
//				if (i > 0) {
//					_move.xTo = divLocals[i] - divLocals[i - 1];
//					_resize.widthTo = _move.xTo;
//				} else {
//					_move.xTo = divLocals[i];
//					_resize.widthTo = divLocals[i];
//				}
				_move.xTo = divLocals[i];
				_move.play();
				_resize.widthTo = divLocals[i];
				_resize.play();
//				divLocals[i] = 0;
				this.dividerBoxes[i].source = _middle_icon_left;
			}
		}
	}
}